package cn.tedu.tmall.front.mall.controller;

import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.front.mall.pojo.vo.GoodsListItemVO;
import cn.tedu.tmall.front.mall.service.IGoodsService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/goods")
@Validated
@Api(tags = "2. 商品管理")
public class GoodsController {
    @Autowired
    private IGoodsService service;

    // TODO 根据ID查询商品

    @GetMapping("/list-by-recommend")
    @ApiOperation("查询推荐的商品列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "条数", defaultValue = "1", paramType = "query", dataType = "int")
    })
    public JsonResult listByRecommend(@Range(min = 1, message = "请提交有效的页码值！") Integer num,
                                      @Range(min = 1, message = "请提交有效的条数！") Integer size) {
        Integer pageNum = num == null ? 1 : num;
        Integer pageSize = num == null ? 1 : size;
        PageData<GoodsListItemVO> pageData = service.listByRecommend(pageNum, pageSize);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/list-by-category")
    @ApiOperation("根据类别查询商品列表")
    @ApiOperationSupport(order = 421)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "商品类别ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "num", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "条数", defaultValue = "1", paramType = "query", dataType = "int")
    })
    public JsonResult listByCategory(@Range(message = "请提交有效的商品类别ID值！") Long categoryId,
                                     @Range(min = 1, message = "请提交有效的页码值！") Integer num,
                                     @Range(min = 1, message = "请提交有效的条数！") Integer size) {

        Integer pageNum = num == null ? 1 : num;
        Integer pageSize = num == null ? 1 : size;
        PageData<GoodsListItemVO> pageData = service.listByCategory(categoryId, pageNum, pageSize);
        return JsonResult.ok(pageData);
    }

    // TODO 搜索商品
}
